<?php

namespace app\admin\controller;

use app\admin\model\ScoreShop as ModelScoreShop;
use app\common\LogService;
use think\facade\Db;

class ScoreShop extends Base
{
    public function index()
    {
        return view();
    }
    public function list()
    {
        $list = ModelScoreShop::list();
        return success($list);
    }
    public function add()
    {
        return view('add');
    }
    public function doAdd()
    {
        $param = input();
        $recordLog = function ($status, $message = '') {
            LogService::record(
                name: '添加积分项目',
                user_id: $this->uid,
                status: $status,
                content: $message == '' ? [] : ['reason' => $message],
                user_type: $this->role

            );
        };
        $res = ModelScoreShop::create($param);
        if ($res) {
            $recordLog(1);
            return success();
        }
        $recordLog(0);
        return error();
    }
    public function edit($id)
    {
        $data = ModelScoreShop::find($id);
        return view('edit', ['data' => $data]);
    }
    public function doEdit()
    {
        $param = input();
        $id = $param['id'];
        $recordLog = function ($status, $message = '') use ($id) {
            LogService::record(
                name: '编辑积分项目',
                user_id: $this->uid,
                target_id: $id,
                status: $status,
                content: $message == '' ? [] : ['reason' => $message],
                user_type: $this->role

            );
        };
        $res = ModelScoreShop::update($param);
        if ($res) {
            $recordLog(1);
            return success();
        }
        $recordLog(0);
        return error();
    }
    public function del($id)
    {
        $recordLog = function ($status, $message = '') use ($id) {
            LogService::record(
                name: '删除积分项目',
                user_id: $this->uid,
                target_id: $id,
                status: $status,
                content: $message == '' ? [] : ['reason' => $message],
                user_type: $this->role

            );
        };
        $res = ModelScoreShop::destroy($id);
        if ($res) {
            $recordLog(1);
            return success();
        }
        $recordLog(0);
        return error();
    }
}
